/**获取元素相对于浏览器的坐标矩形
 * https://www.cnblogs.com/zyyhxbs/p/12376181.html
 * */

//读取参数
let args = arguments
let css = args[0] //目标元素

function getOffsetSum(elem) {
    let top = 0, left = 0
    while (elem) {
        top = top + parseInt(elem.offsetTop)
        left = left + parseInt(elem.offsetLeft)
        elem = elem.offsetParent
    }
    return {top: top, left: left}
}

if (css) {
    let selector = document.querySelector(css);
    if (selector) {
        if (selector.getBoundingClientRect) {
            let rect = selector.getBoundingClientRect();
            return [rect.x, rect.y, rect.width, rect.height]
        } else {
            let location = getOffsetSum(selector)
            return [location.left, location.top, selector.width, selector.height]
        }
    }
}

